#include "city_path.h"
#include "city_map.h"

#include "graphics/graphics_util.h"

#include "font/font_manager.h"
#include "dd/dd_man.h"
#include "util/timer.h"				// log
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    
#include "script/scripthelper.h"
#include <luabind/luabind.hpp>

using namespace city;


CityPath::CityPath(CityMap* pScene, int Type) :
	m_pScene(pScene),
	m_FromNode(-1),
	m_ToNode(-1),
	m_Type(Type)
{

}

CityPath::~CityPath()
{

}

void	CityPath::CreateFromSpline(const math::SplineCache2D& Spline)
{	
	m_Spline.Clear();
	for (int i=0; i<(int)Spline.GetNodeC(); i++)
	{
		m_Spline.InsertNode(Spline.GetNode(i)->Pos);
	}
}


void	CityPath::RenderDebug(const math::Vec2& Offset)
{
	COLOR c = ColorRGBA(255, 0, 0, 255);
	if (m_Type == 1)	c = ColorRGBA(255, 255, 255, 255);
	dd::Manager::Instance().AddSpline2(&m_Spline, 0.01f, c, false, m_pScene->GetOffset());
}

void	CityPath::Tick(float dt)
{
}

math::Vec2	CityPath::GetPos()
{
	math::Vec2	Pos = m_Spline.GetPos(0, 0.01f);
	math::Vec2	P;
	if (!m_Spline.SplineHitTest(Pos, &P, 10/800.0f))
	{
		int j=0;
	}
	return Pos;
}

bool		CityPath::HitTest(const math::Vec2& Pos, float Radius)
{
	math::Vec2	P;
	return m_Spline.SplineHitTest(Pos, &P, Radius);
}

bool		CityPath::TestEnds(int From, int To)
{
	return (From==m_FromNode && To == m_ToNode) || (From==m_ToNode && To == m_FromNode);
}

